#include "WidgetWeatherFull.h"



WidgetWeatherFull::WidgetWeatherFull(const char *pRootPath, const char *pIniUIFilename, const char *pIniDriverMappingFilename, const char *pIniVehiculeClassMappingFilename)
{
	needToLoadImageRadar = false;
	initialized = false;
	rootPath = pRootPath; 

	nbProfiles = GetPrivateProfileInt("Profiles", "count", 0, pIniUIFilename);
	for (int i = 0 ; i < nbProfiles; i++) {
		sprintf(tmp, "Profile.%d", (i+1));
		profiles[i].widgetOffsetX = (signed int)GetPrivateProfileInt(tmp, "WeatherFull.offsetX", 0, pIniUIFilename);
		profiles[i].widgetOffsetY = (signed int)GetPrivateProfileInt(tmp, "WeatherFull.offsetY", 0, pIniUIFilename);
		profiles[i].hide = GetPrivateProfileInt(tmp, "WeatherFull.hide", 0, pIniUIFilename) != 0;
	}

	//std::string str = rootPath;
	GetPrivateProfileString("UI Widgets", "WeatherFull.ini", "", tmp, 999, pIniUIFilename);
	//str.append(tmp);
	sprintf(iniUIFilename, "%s%s", rootPath, tmp);
	//iniUIFilename = str.c_str(); //pIniUIFilename; 
	available = GetPrivateProfileInt("WeatherFull", "available", 0, iniUIFilename) != 0;

}

void WidgetWeatherFull::init(const ScreenInfoV01 &info, float ratio, float ratioWidth, float ratioHeight, Sprite spriteList[]) {
	if (!available) {
		initialized = true;
		return;
	}

	for (int i = 0 ; i < nbProfiles; i++) {
		profiles[i].widgetOffsetX *= ratioWidth;
		profiles[i].widgetOffsetY *= ratioHeight;
	}

	int posX = (int)(ratioWidth * GetPrivateProfileInt("WeatherFull", "image.left", 40, iniUIFilename));//40;
	int posY = (int)(ratioHeight * GetPrivateProfileInt("WeatherFull", "image.top", 550, iniUIFilename));//550;

	//LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	

	bgImageData.spriteIndex = GetPrivateProfileInt("WeatherFull", "image.id", 0, iniUIFilename) -1;
	if (bgImageData.spriteIndex > -1) {
		bgImageData.left = posX;
		bgImageData.top = posY;
		bgImageData.width = (int)(GetPrivateProfileInt("WeatherFull", "image.width", (int)spriteList[bgImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
		bgImageData.height = (int)(GetPrivateProfileInt("WeatherFull", "image.height", (int)spriteList[bgImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		bgImageData.colorIndex = GetPrivateProfileInt("WeatherFull", "image.color.id", 1, iniUIFilename) -1;
	}

	radarImageData.left = (int)(posX + GetPrivateProfileInt("WeatherFull.Radar", "image.left", 0, iniUIFilename) * ratioWidth);
	radarImageData.top = (int)(posY + GetPrivateProfileInt("WeatherFull.Radar", "image.top", 0, iniUIFilename) * ratioHeight);
	radarImageData.width = (int)(GetPrivateProfileInt("WeatherFull.Radar", "image.width", 0, iniUIFilename) * ratioWidth);
	radarImageData.height = (int)(GetPrivateProfileInt("WeatherFull.Radar", "image.height", 0, iniUIFilename) * ratioHeight);
	radarImageData.colorIndex = GetPrivateProfileInt("WeatherFull.Radar", "image.color.id", 1, iniUIFilename) -1;

	initialized = true;
}
void WidgetWeatherFull::setTextureFromStream() { 
	//memcpy(&imageDataStreamSize, &buf[1], 4);
	//memcpy(&imageDataStream, &buf[5], imageDataStreamSize);			
	needToLoadImageRadar = true;
}
void WidgetWeatherFull::render(int profileIndex, const ScreenInfoV01 &info, const LPD3DXFONT *fontList, const D3DCOLOR *colorList, Sprite spriteList[]) { 
	if (!available) return;
	if (gReplayIsActive) return;
	if (profiles[profileIndex].hide) return;

	LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	if (bgImageData.spriteIndex > -1) {
		spriteList[bgImageData.spriteIndex].setPosition(bgImageData.left + profiles[profileIndex].widgetOffsetX, bgImageData.top + profiles[profileIndex].widgetOffsetY);
		spriteList[bgImageData.spriteIndex].setSize(bgImageData.width, bgImageData.height);
		spriteList[bgImageData.spriteIndex].render(dev, colorList[bgImageData.colorIndex], 100);
	}

	if (needToLoadImageRadar) {
		needToLoadImageRadar = false;
		radarImage.loadSpriteFromBuffer(dev, imageDataStream, imageDataStreamSize);
		//radarImage.loadSprite(dev, "C:/Users/Amphiprion/test.jpg");
	}
	radarImage.setPosition(radarImageData.left + profiles[profileIndex].widgetOffsetX, radarImageData.top + profiles[profileIndex].widgetOffsetY);
	radarImage.setSize(radarImageData.width, radarImageData.height);
	radarImage.render(dev, colorList[radarImageData.colorIndex], 100);

}

WidgetWeatherFull::~WidgetWeatherFull(void)
{
}
